//
//  DDForgetPWVC.m
//  dingdang
//
//  Created by Jiangxp on 15/6/29.
//  Copyright (c) 2015年 阮 沧晖. All rights reserved.
//

#import "DDForgetPWVC.h"
#import "StringUtil.h"
#import "UICommonFunc.h"
#import "DDNetWorkAPIClient.h"

@interface DDForgetPWVC ()
{
    NSUInteger remainSeconds;
    NSTimer *timer;
    
    NSString *orgTitle;
}
@end

@implementation DDForgetPWVC

- (void)createUI
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    //自定义TAB BAR UI
    [self addNavigatorTitle:NSLocalizedString(@"找回密码", nil) parent:self.view];
    self.barViewControl.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_Background"]];
    
    [self autoTFHeight:_phoneCode];
    [self autoTFHeight:_phoneNum];
    [self autoTFHeight:_password];
    [self autoTFHeight:_password2];
    
    self.smsCodeButton.layer.cornerRadius = 10;
    self.smsCodeButton.backgroundColor = [UIColor whiteColor];
    self.smsCodeButton.layer.borderColor = RGB(27, 142, 228).CGColor;
    self.smsCodeButton.layer.borderWidth = 1.0;
    self.submitButton.layer.cornerRadius = 10;
    
}
- (void)autoTFHeight:(UITextField *)tf
{
    CGRect rect = tf.frame;
    rect.size.height = 50;
    tf.frame = rect;
}

-(void)createData
{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createUI];
    [self createData];
    DDSharedUser *user = [DDSharedUser shared];
    _phoneNum.text = [user userDefaultValueForKey:kUsername];
    [self AddLeftBtnAction:@"" normal:@"top_返回" selected:@"" action:^{
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)getPhoneCode:(id)sender {
    if (![StringUtil checkPhoneNumInput:_phoneNum.text ]) {
        [UICommonFunc showAlert:@"手机号码输入有误！"];
        return;
    }
    DDNetWorkAPIClient *network = [[DDNetWorkAPIClient alloc] init];
    [network userPhoneCode:_phoneNum.text completionBlock:^(BOOL isSuccess, NSString *message, NSString *status)
    {
        if (isSuccess) {
            [self.smsCodeButton startTimer:60];
        }else{
            [UICommonFunc showAlert:message];
        }
    }];
}

- (IBAction)submit:(id)sender {
    if (![StringUtil checkPhoneNumInput:_phoneNum.text ]) {
        [UICommonFunc showAlert:@"手机号码输入有误！"];
        return;
    }
    
    if (![_password.text isEqualToString:_password2.text]) {
        [UICommonFunc showAlert:@"两次输入的密码不一致！"];
        return;
    }
    
    if (_password.text.length > 14 || _password.text.length < 6) {
        [UICommonFunc showAlert:@"请输入6-14位密码！"];
        return;
    }
    
    DDSharedUser *user = [DDSharedUser shared];
    
    [user updateUserDefaults:_phoneNum.text forKey:kUsername];
    [user updateUserDefaults:_password.text forKey:kPassword];
    if (_phoneCode.text.length == 0) {
        [UICommonFunc showAlert:@"请输入验证码！"];
        return;
    }
    
    DDNetWorkAPIClient *network = [[DDNetWorkAPIClient alloc] init];
    NSString *passwordType = @"1"; // 类型，1-登录密码 2-支付密码
    [network resetPassword:_phoneNum.text phoneCode:_phoneCode.text passwordType:passwordType password:_password.text completionBlock:^(BOOL isSuccess, NSString *message, NSString *status) {
        [UICommonFunc showAlert:message];
        if (isSuccess) {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

- (IBAction)clickBackground:(id)sender {
    [sender endEditing:YES];
}

@end
